//
//  MyBankCardViewController.m
//  HuaMuJinRong
//
//  Created by 吴建东 on 16/4/20.
//  Copyright © 2016年 EIMS. All rights reserved.
//

#import "MyBankCardViewController.h"
//#import "VerifyMobileViewController.h"
#import "AddBankCardViewController.h"

@interface MyBankCardViewController ()<HTTPClientDelegate>
{
    NSString *_mobileStr;
    NSString *_bankID;
}
@property (weak, nonatomic) IBOutlet UIView      *bankInfoBackView;
@property (weak, nonatomic) IBOutlet UIView      *mobileInfoBackView;
@property (weak, nonatomic) IBOutlet UILabel     *mobileNumLabel;
@property (weak, nonatomic) IBOutlet UILabel     *mobileNumValue;
@property (weak, nonatomic) IBOutlet UIImageView *bankLogoImageView;
@property (weak, nonatomic) IBOutlet UILabel     *bankNameLabel;
@property (weak, nonatomic) IBOutlet UILabel     *bankNumLabel;
@property (weak, nonatomic) IBOutlet UILabel     *realNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *veriryValue;
@property (weak, nonatomic) IBOutlet ClickButton *changeBankButton;

@property (nonatomic, strong) NetWorkClient  *requestClient;
@property (nonatomic, assign) BOOL           isLoading;
@property (nonatomic, assign) NSInteger      requestType;

@end

@implementation MyBankCardViewController
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (_requestClient != nil) {
        [_requestClient cancel];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = KbackgroundColor;
    [self initNavBarTitle:@"实名认证信息" andLeftItemName:@"返回" andRightItemName:@""];
    
    [self initView];
    
    [self initData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editBankCardSuccess) name:@"editBankCardSuccess" object:nil];
}

- (void)back {
    if (_isComeAdd) {
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 3] animated:YES];
    }else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)editBankCardSuccess {
    [self requestData];
}

- (void)initView {
    _bankInfoBackView.layer.cornerRadius = KAdaptNum(3.0);
    _bankNameLabel.font = KFontSize(15.0);
    _bankNumLabel.font = KFontSize(16.0);
    _realNameLabel.font = KFontSize(13.0);
    _mobileNumValue.font = KFontSize(14.0);
    _mobileNumLabel.font = KFontSize(14.0);
    _veriryValue.font = KFontSize(12);
    _changeBankButton.titleLabel.font = KFontSize(16.0);
    _changeBankButton.clipsToBounds = YES;
    _changeBankButton.layer.cornerRadius = kCornerRadius;
}

- (IBAction)changeBankButtonClick:(id)sender {
    if (_isLoading) {
        [SVProgressHUD showImage:nil status:@"正在加载数据，请稍候..."];
        return;
    }
    AddBankCardViewController *addBank = [[AddBankCardViewController alloc] init];
    addBank.editType = 2;
    addBank.bankId = _bankID;
    [self.navigationController pushViewController:addBank animated:YES];
}
- (IBAction)dealMobileViewTAp:(id)sender {
    
    //先验证手机号码
//    VerifyMobileViewController *verifyMobile = [[VerifyMobileViewController alloc] init];
//    verifyMobile.viewType = ChangeBankMobile;
//    verifyMobile.naviTitle = @"更改预留手机号";
//    verifyMobile.mobileStr = _mobileStr;
//    [self.navigationController pushViewController:verifyMobile animated:YES];
}

#pragma mark - 请求数据
- (void)initData {
    
    [self requestData];
}



- (void)requestData {
    _requestType = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@"516" forKey:@"OPT"];
    [parameters setObject:@"" forKey:@"body"];
    [parameters setObject:AppDelegateInstance.userInfo.userId forKey:@"id"];
    
    if (_requestClient == nil) {
        _requestClient = [[NetWorkClient alloc] init];
        _requestClient.delegate = self;
    }
    
    [_requestClient requestGet:@"app/services" withParameters:parameters withViewController:self];
}


#pragma - 网络数据回调代理
- (void)startRequest {
    _isLoading = YES;
}

// 返回成功
- (void)httpResponseSuccess:(NetWorkClient *)client dataTask:(NSURLSessionDataTask *)task didSuccessWithObject:(id)obj {
    _isLoading = NO;
    NSDictionary *dics = obj;
    DLOG(@"dic=%@",dics);
    if ([[NSString stringWithFormat:@"%@",[dics objectForKey:@"error"]] isEqualToString:@"-1"]) {
        if (_requestType == 1) {
            NSString *realName = [NSString stringWithFormat:@"%@",dics[@"realName"]];
            NSString *idNumber = [NSString stringWithFormat:@"%@",dics[@"idNumber"]];
            _mobileNumLabel.text = [NSString stringWithFormat:@"%@**  %@*******%@",[realName substringToIndex:1],[idNumber substringToIndex:7],[idNumber substringFromIndex:idNumber.length - 1]];
            
            NSArray *userBanks = [dics objectForKey:@"userBanks"];
            if (userBanks.count) {
                NSDictionary *dataDict = userBanks[0];
                if (dataDict) {
                    
                    NSString *account = [NSString stringWithFormat:@"%@",dataDict[@"account"]];
                    _bankNumLabel.text = [NSString stringWithFormat:@"%@***********%@",[account substringToIndex:4],[account substringFromIndex:account.length-3]];
                    NSString *accountName = [NSString stringWithFormat:@"%@",dataDict[@"accountName"]];
                    _realNameLabel.text = [NSString stringWithFormat:@"持卡人：%@**",[accountName substringToIndex:1]];
                    
                    _bankID = [NSString stringWithFormat:@"%@",dataDict[@"id"]];
                    
                    _bankNameLabel.text = [NSString stringWithFormat:@"%@",dataDict[@"bankName"]];
                    NSString *bankNameStr = [NSString stringWithFormat:@"%@",dataDict[@"bankName"]];
                    if ([bankNameStr rangeOfString:@"CCB"].length != 0) {
                        _bankNameLabel.text = @"建设银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#425190"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_jsyh"];
                    }else if ([bankNameStr rangeOfString:@"ICBC"].length != 0) {
                        _bankNameLabel.text = @"工商银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#d04545"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_gsyh"];
                    }else if ([bankNameStr rangeOfString:@"ABC"].length != 0) {
                        _bankNameLabel.text = @"农业银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#50a993"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_nyyh"];
                    }else if ([bankNameStr rangeOfString:@"BOC"].length != 0) {
                        _bankNameLabel.text = @"中国银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#d04545"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_zgyh"];
                    }else if ([bankNameStr rangeOfString:@"BCOM"].length != 0) {
                        _bankNameLabel.text = @"交通银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#3b4469"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_jtyh"];
                    }else if ([bankNameStr rangeOfString:@"CIB"].length != 0) {
                        _bankNameLabel.text = @"兴业银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#425190"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_xyyh"];
                    }else if ([bankNameStr rangeOfString:@"CITIC"].length != 0) {
                        _bankNameLabel.text = @"中信银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#ec4552"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_zxyh"];
                    }else if ([bankNameStr rangeOfString:@"CEB"].length != 0) {
                        _bankNameLabel.text = @"光大银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#8b4ba3"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_gdyh"];
                    }else if ([bankNameStr rangeOfString:@"SZPAB"].length != 0) {
                        _bankNameLabel.text = @"平安银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#50a978"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_payh"];
                    }else if ([bankNameStr rangeOfString:@"PSBC"].length != 0) {
                        _bankNameLabel.text = @"邮政银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#50a978"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_yzyh"];
                    }else if ([bankNameStr rangeOfString:@"BOS"].length != 0) {
                        _bankNameLabel.text = @"上海银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#e3c72c"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_shyh"];
                    }else if ([bankNameStr rangeOfString:@"SPDB"].length != 0) {
                        _bankNameLabel.text = @"浦发银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#3b4469"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_pfyh"];
                    }else if ([bankNameStr rangeOfString:@"CMBC"].length != 0) {
                        _bankNameLabel.text = @"民生银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#50a978"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_msyh"];
                    }
//                    else if ([bankNameStr rangeOfString:@""].length != 0) {
//                        _bankNameLabel.text = @"招商银行";
//                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#ec4552"];
//                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_zsyh"];
//                    }
                    else if ([bankNameStr rangeOfString:@"GDB"].length != 0) {
                        _bankNameLabel.text = @"广发银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#ec4552"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_gfyh"];
                    }else if ([bankNameStr rangeOfString:@"BCCB"].length != 0) {
                        _bankNameLabel.text = @"北京银行";
                        _bankInfoBackView.backgroundColor = [ColorTools colorWithHexString:@"#ec4552"];
                        _bankLogoImageView.image = [UIImage imageNamed:@"BankLogo_bjyh"];
                    }

                }
            }
            
            
        }else if (_requestType == 2) {
            [SVProgressHUD showSuccessWithStatus:[dics objectForKey:@"msg"]];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
    }else if ([[NSString stringWithFormat:@"%@",[dics objectForKey:@"error"]] isEqualToString:@"-2"]) {
        
        [ReLogin outTheTimeRelogin:self];
    }else {
        
        [SVProgressHUD showImage:nil status:[dics objectForKey:@"msg"]];
    }
    
}

// 返回失败
- (void)httpResponseFailure:(NetWorkClient *)client dataTask:(NSURLSessionDataTask *)task didFailWithError:(NSError *)error {
    _isLoading = NO;
}

// 无可用的网络
- (void)networkError {
    //  [SVProgressHUD showErrorWithStatus:@"网络不给力，请检查您的网络设置"];
    _isLoading = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
